#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> vv;
        vv.resize(0);
        sort(nums.begin(), nums.end());
        int n = nums.size();
        for (int i = 0; i < n - 2; i++)
        {
            if (nums[i] > 0)
                break;
            int left = i + 1;
            int right = n - 1;
            while (left < right)
            {
                if (nums[i] + nums[left] + nums[right] == 0)
                {
                    if (vv.size() == 0)
                        vv.push_back({ nums[i], nums[left], nums[right] });
                    else
                    {
                        bool b = true;
                        for (int x = 0; x < vv.size(); x++)
                        {
                            if (vv[x][0] == nums[i] && vv[x][1] == nums[left] && vv[x][2] == nums[right])
                            {
                                b = false;
                                break;
                            }
                        }
                        if (b)
                            vv.push_back({ nums[i], nums[left], nums[right] });
                    }
                    left++;
                    right--;
                }
                else if (nums[i] + nums[left] + nums[right] < 0)
                    left++;
                else
                    right--;
            }
        }
        return vv;
    }
};

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> vv;
        vv.resize(0);
        sort(nums.begin(), nums.end());
        int n = nums.size();
        for (int i = 0; i < n - 2; i++)
        {
            if (nums[i] > 0)
                break;
            int left = i + 1;
            int right = n - 1;
            if (i > 0 && nums[i] == nums[i - 1])
                continue;
            while (left < right)
            {
                int a = nums[left];
                int b = nums[right];
                if (nums[i] + nums[left] + nums[right] == 0)
                {
                    vv.push_back({ nums[i], nums[left], nums[right] });
                    left++;
                    while (left < right)
                    {
                        if (a != nums[left])
                            break;
                        else
                            left++;
                    }
                    right--;
                    while (left < right)
                    {
                        if (b != nums[right])
                            break;
                        else
                            right--;
                    }
                }
                else if (nums[i] + nums[left] + nums[right] < 0)
                {
                    left++;
                    while (left < right)
                    {
                        if (a != nums[left])
                            break;
                        else
                            left++;
                    }
                }
                else
                {
                    right--;
                    while (left < right)
                    {
                        if (b != nums[right])
                            break;
                        else
                            right--;
                    }
                }
            }
        }
        return vv;
    }
};